﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Globalization
Imports System.ComponentModel.DataAnnotations

Namespace Infrastructure

    <AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property, AllowMultiple:=False, Inherited:=True)>
    Public NotInheritable Class ValidatePasswordLengthAttribute
        Inherits ValidationAttribute
        Private Const DefaultErrorMessage As String = "'{0}' must be at least {1} characters long."

        Private ReadOnly minCharacters As Integer = Membership.Provider.MinRequiredPasswordLength

        Public Sub New()
            MyBase.New(DefaultErrorMessage)
        End Sub

        Public Overrides Function FormatErrorMessage(ByVal name As String) As String
            Return String.Format(CultureInfo.CurrentUICulture, Me.ErrorMessageString, name, Me.minCharacters)
        End Function

        Public Overloads Overrides Function IsValid(ByVal value As Object) As Boolean
            Dim valueAsString = TryCast(value, String)

            Return (valueAsString IsNot Nothing) AndAlso (valueAsString.Length >= Me.minCharacters)
        End Function
    End Class
End Namespace